import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    token: uni.getStorageSync('token') || '',
    userInfo: uni.getStorageSync('userInfo') || null
  }),

  actions: {
    // 设置token
    setToken(token) {
      this.token = token
      uni.setStorageSync('token', token)
    },

    // 获取token
    getToken() {
      return this.token
    },

    // 设置用户信息
    setUserInfo(userInfo) {
      this.userInfo = userInfo
      uni.setStorageSync('userInfo', userInfo)
    },

    // 获取用户信息
    getUserInfo() {
      return this.userInfo
    },

    // 清除登录信息
    clearLogin() {
      this.token = ''
      this.userInfo = null
      uni.removeStorageSync('token')
      uni.removeStorageSync('userInfo')
    }
  }
}) 